/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package virt;

import java.io.IOException;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.libvirt.Connect;
import org.libvirt.Domain;
import org.libvirt.LibvirtException;
import org.libvirt.Network;

/**
 *
 * @author gravena
 */
public class VirtualMachineFactory {

    public static String getVirtualNetworkXML(int ID) {
        String XML = "<network>" +
        "<name>nat " + ID + "</name>" +
        "<bridge name=\"virbr" + ID + "\" />" +
        "<forward mode=\"nat\"/>" +
        "<ip address=\"192.168.12" + ID + ".1\" netmask=\"255.255.255.0\">" +
          "<dhcp>" +
            "<range start=\"192.168.12" + ID + ".2\" end=\"192.168.12" + ID + ".254\" />" +
          "</dhcp>" +
        "</ip>" +
      "</network>";

        return XML;
    }
    public static String getVirtualMachineXML(UUID ID) {
        String XML = "<domain type='kvm'>" +
  "<name>VM " + (ID) + "</name>" +
  "<uuid></uuid>" +
  "<memory>262144</memory>" +
  "<currentMemory>262144</currentMemory>" +
  "<vcpu>1</vcpu>" +
  "<os>" +
    "<type arch='x86_64' machine='pc-0.14'>hvm</type>" +
    "<boot dev='hd'/>" +
    "<bootmenu enable='no'/>" +
  "</os>" +
  "<features>" +
    "<acpi/>" +
    "<apic/>" +
    "<pae/>" +
  "</features>" +
  "<clock offset='utc'/>" +
  "<on_poweroff>destroy</on_poweroff>" +
  "<on_reboot>restart</on_reboot>" +
  "<on_crash>restart</on_crash>" +
  "<devices>" +
    "<emulator>/usr/bin/kvm</emulator>" +
    "<disk type='file' device='disk'>" +
      "<driver name='qemu' type='raw'/>" +
      "<source file='/home/gravena/COMEVIM/Modulos/disks/disk" + (ID) + ".img'/>" +
      "<target dev='hda' bus='ide'/>" +
      "<alias name='ide0-0-0'/>" +
      "<address type='drive' controller='0' bus='0' unit='0'/>" +
    "</disk>" +
    "<disk type='file' device='cdrom'>" +
      "<driver name='qemu' type='raw'/>" +
      "<target dev='hdc' bus='ide'/>" +
      "<readonly/>" +
      "<alias name='ide0-1-0'/>" +
      "<address type='drive' controller='0' bus='1' unit='0'/>" +
    "</disk>" +
    "<controller type='ide' index='0'>" +
      "<alias name='ide0'/>" +
      "<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>" +
    "</controller>" +
    "<interface type='network'>" +
      "<mac address='52:54:00:21:2f:" + 10 + "'/>" +
      "<source network='default'/>" +
      "<target dev='vnet0'/>" +
      "<model type='rtl8139'/>" +
      "<alias name='net0'/>" +
      "<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>" +
    "</interface>" +
    "<serial type='pty'>" +
      "<source path='/dev/pts/0'/>" +
      "<target port='0'/>" +
      "<alias name='serial0'/>" +
    "</serial>" +
    "<console type='pty' tty='/dev/pts/0'>" +
      "<source path='/dev/pts/0'/>" +
      "<target type='serial' port='0'/>" +
      "<alias name='serial0'/>" +
    "</console>" +
    "<input type='mouse' bus='ps2'/>" +
    "<graphics type='vnc' port='5900' autoport='yes'/>" +
    "<sound model='ac97'>" +
      "<alias name='sound0'/>" +
      "<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>" +
    "</sound>" +
    "<video>" +
      "<model type='cirrus' vram='9216' heads='1'/>" +
      "<alias name='video0'/>" +
      "<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>" +
    "</video>" +
    "<memballoon model='virtio'>" +
      "<alias name='balloon0'/>" +
      "<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>" +
    "</memballoon>" +
  "</devices>" +
"</domain>";        
        return XML;
    }

    public synchronized static Domain createVM(UUID id) {
        Connect conn=null;
        try {

            conn = new Connect("qemu:///system");
            Domain domain=null;
            Network network=null;
            try {
                Runtime.getRuntime().exec("cp disks/disk0.img disks/disk" + id + ".img");
                domain = conn.domainCreateLinux(VirtualMachineFactory.getVirtualMachineXML(id), 0);
                //network = conn.networkCreateXML(VirtualMachineFactory.getVirtualNetworkXML(VirtualMachineFactory.ID));

            } catch (IOException ex) {
                System.out.println("Erro na cópia do disco");
            }

            return domain;
        } catch (LibvirtException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Erro libvirt");
            return null;
        }
    }

}
